/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.je.justforme.http;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jeddy
 */
public class DefaultProperties {

    public static final String PROP_AUTH_URL = "vons.url.login";
    private static final String PROP_INIT = "justforme.init";
    public static final String PROP_UNAME = "user.name";
    public static final String PROP_PASSWORD = "user.password";
    public static final String PROP_UNAME_FORM_ID = "user.uname.form.id";
    public static final String PROP_PASSWORD_FORM_ID = "user.password.form.id";
    /** comma semicolon list of form data*/
    public static final String PROP_STATIC_FORM_DATA = "static.form.data";
    public static final String PROP_COUPON_CENTER_URL = "coupon.center";
    public static final String PROP_PERSONALIZED_DEALS_URL = "personalized.deals";
    public static final String PROP_CLUB_SPECIALS_URL = "club.specials";
    public static final String PROP_ADD_CLIPPING_URL = "add.specials";
    private static final File propFile = new File(System.getProperty("user.home") + File.separator + ".justforme");
    private static Properties props = null;

    public static Properties getDefaultProps() {
        if (props == null) {
            try {
                loadSettings();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(DefaultProperties.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(DefaultProperties.class.getName()).log(Level.SEVERE, null, ex);
            }
            setIfMissing(props, PROP_AUTH_URL, "https://auth.vons.com/opensso/UI/Login");
            setIfMissing(props, PROP_UNAME_FORM_ID, "IDToken1");
            setIfMissing(props, PROP_PASSWORD_FORM_ID, "IDToken2");
            setIfMissing(props, PROP_STATIC_FORM_DATA, "SunQueryParamsString: ;encoded:true;arg:newsession;fakePassword:8-12 characters");
            setIfMissing(props, PROP_COUPON_CENTER_URL, "http://www.vons.com/J4UProgram1/services/program/CC/offer/allocations?_dc=$time&hierarchies=y");
            setIfMissing(props, PROP_PERSONALIZED_DEALS_URL, "http://www.vons.com/J4UProgram1/services/program/PD/offer/allocations?_dc=$time&hierarchies=y");
            setIfMissing(props, PROP_CLUB_SPECIALS_URL, "http://www.vons.com/J4UProgram1/services/program/YCS/offer/allocations?_dc=$time&hierarchies=y");
            setIfMissing(props, PROP_ADD_CLIPPING_URL, "http://www.vons.com/Clipping1/services/clip/offers");

        }
        return props;
    }

    public static void saveSettings() {
        if (props != null) {
            try {
                saveSettings(props);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void saveSettings(Properties props) throws FileNotFoundException, IOException {
        FileOutputStream out = new FileOutputStream(propFile);
        props.store(out, null);
    }

    private static void loadSettings() throws FileNotFoundException, IOException {
        props = new Properties();
        System.out.println("Loading from: " + propFile.getAbsolutePath());
        props.load(new FileInputStream(propFile));
    }

    private static void setIfMissing(Properties props, String key, String val) {
        if (!props.contains(key)) {
            props.setProperty(key, val);
        }
    }
}
